package com.chixing.day09_innerclass;

import java.util.Arrays;

public class StringStudy {
    public static void main(String[] args) {
        String word = "Strings are constant; their values cannot be changed after they  are created";
//        1. 将字符串转换成 char数组

        System.out.println(word.toCharArray());

//        2. 获得字符串的第一个字符，第5个字符
        System.out.println(word.charAt(0));
        System.out.println(word.charAt(4));

//        3. 判断里面是否包含 "not"
        System.out.println(word.contains("not"));
//        4. 获得字符串的长度
        System.out.println(word.length());
//        5. 将字符串转换成 byte数组
        System.out.println(Arrays.toString(word.getBytes()));
//        6. 获得字符串中第一次出现字母e 的下标
        System.out.println(word.indexOf("e"));
//        7. 获得字符串中第一次出现 "an" 的下标
        System.out.println(word.indexOf("an"));
//        8. 获得字符串中最后一次出现 "e" 的下标
        System.out.println(word.lastIndexOf("e"));
//        9. 将字符串中的字母 'a' 替换成 'A'
        System.out.println(word.replaceAll("a","A"));
//        10. 将字符串中的"er" 替换成 'ER'
        System.out.println(word.replaceAll("er","ER"));
//        11. 将字符串全部转换成大写
        System.out.println(word.toUpperCase());
//        12. 将字符串全部转换成小写
        System.out.println(word.toLowerCase());
//        13. 截取前5个字符串   === 截取子字符串
        System.out.println(word.substring(0,5));
//        14. 截取第6-10 个字符串
        System.out.println(word.substring(5,10));
//        15. 截取后6 个字符串
        System.out.println(word.substring(word.length()-6,word.length()));
//        16. 在字符串后拼接单词"hello"
        System.out.println(word.concat("hello"));
//        17. 获得每个单词 "Strings","are","constant;","their".....(即不输出中间的空格符号)
        System.out.println(Arrays.toString(word.split(" ")));
//       18. 判断是否以"Str"开头
        System.out.println(word.startsWith("Str"));
//        19. 判断是否以"Str"结束
        System.out.println(word.endsWith("Str"));
//        20.输出原来字符串 每个字符
        for (int i = 0;i<word.length();i++){
            System.out.print(word.charAt(i));
        }




        //s1 在字符串常量池中创建一个hello 然后s1再创建一个good s1断开hello
        String s1 = "hello";
        s1 = "good";
        //s2 先在堆中创建一个code 再到常量池中创建一个code s3在常量池中寻找是否有code  并且连接code
        String s2 = new String("code");
        String s3 = "code";
        //s4 在常量池中寻找file  没有就创建一个file 并链接 s5 也在常量池中寻找 并链接
        String s4 = "file";
        String s5 = "file";

        /**
         * String s1 ="a";
         * String s2 ="b";
         *
         *   s1+s2 ===> new String("ab") 堆
         *   s1.concat(s2) ===> new String("ab") 堆
         *
         *   "a" + "b" ===>在常量池中 放一份"ab"
         *
         *
         */
        String str1 = "hello";
        String str2 = "world";
        String str3 = str1 + str2;  // new String("helloworld")
        String str4 = "helloworld";
        System.out.println("str3 == str4 :" + (str3 == str4)); //false


        String str0 = "hello" + "world"; // "helloworld" 到常量池中匹配
        System.out.println("str0 == str4 :" + (str0 == str4)); // true


        String str5 = str1 + str2; // new String("helloworld")
        System.out.println("str3 == str5 :" + (str3 == str5)); //false

        String str6 = str1.concat(str2); // new String( "helloworld")
        System.out.println("str3 == str6 :" + (str3 == str6));
    }
}
